package LeetcodeTest_forty_five;
//LCP 19. 秋叶收藏集
public class minimumOperations
{
    public static void main(String[] args)
    {
        String str="ryr";
        System.out.println(solutions.minimumOperations(str));
    }
}
class solutions
{
    public static int minimumOperations(String leaves)
    {
        int[] sumRed=new int[leaves.length()];
        int i,j,count,result,n;
        int count1,count2,count3;
        n=leaves.length();
        result=Integer.MAX_VALUE;
        if(leaves.charAt(0)=='r')
            sumRed[0]=1;
        else
            sumRed[0]=0;
        for(i=1;i<leaves.length();i++)
        {
            if(leaves.charAt(i)=='r')
            {
                sumRed[i] += sumRed[i - 1] + 1;
            }
            else
                sumRed[i]=sumRed[i-1];
        }
        for(i=1;i<leaves.length()-1;i++)
        {
            for(j=i;j<leaves.length()-1;j++)
            {
                count1=i-sumRed[i-1];
                count2=sumRed[j]-sumRed[i-1];
                count3=n-1-j+sumRed[j]-sumRed[n-1];
                count=count1+count2+count3;
                result=Math.min(count,result);
            }
        }
        return result;
    }
}